package com.xl.core.util.zoom;

import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import com.google.gson.Gson;
import com.xl.core.util.httpclient.HttpRequest;
import com.xl.core.util.zoom.model.CreateMeeting;
import com.xl.core.util.zoom.model.GetMeeting;
import com.xl.core.util.zoom.model.MeetingDetail;
import com.xl.core.util.zoom.model.MeetingList;
import com.xl.core.util.zoom.model.MeetingUserReport;
import com.xl.core.util.zoom.model.ZoomDelete;
import com.xl.core.util.zoom.model.ZoomEnd;
import com.xl.core.util.zoom.model.ZoomUpdate;
import com.xl.core.util.zoom.model.ZoomUser;
import com.xl.core.util.zoom.model.ZoomUserDelete;
import com.xl.web.service.zoom.InitParam;
/**
 * Zoom resetful API
 * @author Administrator
 *
 */
public class ZoomRestAPI1 {

	protected static Logger logger = Logger.getLogger(ZoomRestAPI.class);
	
	public     final static String API_KEY = "XB6j1sX1R6GzNTH8XWF69A";
	public     final static  String API_SECRET = "Wrv9j0Jh1OEoDla3wkSqa2L1XdTXHMaKPuIt";
	private final static String USER_ID = "4pKJR_1HSO6flrl_6AieKA";
	private final static String TOKEN = "yk6pTo-RrSe280IbGAnxXCegaUgaShwOKJUbYrcgs0A.BgMgenh3SXhNdUlvNjEweUlwMmdLK21rNTgyNjUvNGgwckJAYWNjNjkyZDJmZDk3NmVhYWRkMzhhYmZlOGY5YTA2ZDc1MDg3OGZiMGNjOTY1YmVjODM3NTM4MzQwYmMzMTAzNQAMM0NCQXVvaVlTM3M9";
	private final static String API_HOST = "http://www.zoomus.cn/";
	private final static String API_MEET_CREATE_INSTANT_NO_PASSWORD = "v1/meeting/create?api_key=%s&api_secret=%s&data_type=JSON&host_id=%s&topic=%s&type=1&option_start_type=%s";
	private final static String API_MEET_CREATE_INSTANT = "v1/meeting/create?api_key=%s&api_secret=%s&data_type=JSON&host_id=%s&topic=%s&type=1&option_start_type=%s&password=%s";
	private final static String API_MEET_SCHEDULE = "v1/meeting/create?api_key=%s&api_secret=%s&data_type=JSON&host_id=%s&topic=%s&type=2&password=%s&start_time=%s&duration=%d&option_jbh=%b&option_start_type=%s&option_use_pmi=%b&timezone=Asia/Shanghai";
	private final static String API_MEET_UPDATE = "v1/meeting/update?api_key=%s&api_secret=%s&data_type=JSON&host_id=%s&id=%s&topic=%s&type=2&password=%s&start_time=%s&duration=%d&option_jbh=%b&option_start_type=%s&option_use_pmi=%b";
	private final static String API_MEET_DELETE = "v1/meeting/delete?api_key=%s&api_secret=%s&data_type=JSON&host_id=%s&id=%s";
	private final static String API_MEET_END = "v1/meeting/end?api_key=%s&api_secret=%s&data_type=JSON&host_id=%s&id=%s";
	private final static String API_MEET_GET = "v1/meeting/get?api_key=%s&api_secret=%s&data_type=JSON&host_id=%s&id=%s&type=2";
	private final static String API_MEET_LIST = "v1/meeting/list?api_key=%s&api_secret=%s&data_type=JSON&host_id=%s&page_size=%d&page_number=%d";
	
	private final static String API_MEET_DETAIL = "https://private.zoomus.cn/v1/metrics/meetingdetail?api_key=%s&api_secret=%s&data_type=JSON&meeting_id=%s&type=2&page_size=%d&page_number=%d";
	
	private final static String API_USER_CREATE = "v1/user/autocreate2?api_key=%s&api_secret=%s&data_type=JSON&type=3&email=%s&password=%s&first_name=%s&last_name=%s&meeting_capacity=%s&dept=%s";
	private final static String API_USER_UPDATE = "v1/user/update?api_key=%s&api_secret=%s&data_type=JSON&type=%d&id=%s&first_name=%s&last_name=%s&meeting_capacity=%s&dept=%s";
	private final static String API_USER_GET = "v1/user/get?api_key=%s&api_secret=%s&data_type=JSON&id=%s";
	private final static String API_USER_GETBYEMAIL = "v1/user/getbyemail?api_key=%s&api_secret=%s&data_type=JSON&email=%s&login_type=%d";
	private final static String API_USER_DELETE = "v1/user/permanentdelete?api_key=%s&api_secret=%s&data_type=JSON&type=1&id=%s&email=%s";
	
	private final static String API_REPORT_GET_USERREPORT = "v1/report/getuserreport?api_key=%s&api_secret=%s&data_type=JSON&user_id=%s&from=%s&to=%s&page_size=%d&page_number=%d";
	/**
	 * 会议详情
	 * @param meetingId
	 * @param pageSize
	 * @param pageNumber
	 * @return
	 */
	public MeetingDetail getMeetingDetail(String meetingId, int pageSize, int pageNumber) {
		try {
			String url = String.format(API_MEET_DETAIL, API_KEY, API_SECRET, 
					meetingId, pageSize, pageNumber);
			
			String response = HttpRequest.sendPost(url, "");
			if(response == null || response.contains("error")) {
				logger.debug("getMeetingDetail failed.");
				return null;
			}
			Gson gson = new Gson();
			MeetingDetail result = gson.fromJson(response,MeetingDetail.class);
			return result;
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 创建即时会议
	 * @param host_id
	 * @param topic
	 * @return
	 */
	public static CreateMeeting createInstantMeeting(String host_id, String topic,String hasPassword,String password) {
		
		try {
			String url ="";
			//需要密码
			if("1".equals(hasPassword)){
			
			  url = String.format(API_HOST+API_MEET_CREATE_INSTANT, API_KEY, API_SECRET, 
					host_id, URLEncoder.encode(topic), "video");
			}else{
			  url = String.format(API_HOST+API_MEET_CREATE_INSTANT_NO_PASSWORD, API_KEY, API_SECRET, 
							host_id, URLEncoder.encode(topic), "video",password);
			}
			String response = HttpRequest.sendPost(url,"");
			/*if(response == null || response.contains("error")) {
				logger.debug("createInstantMeeting failed.");
				return null;
			}*/
			Gson gson = new Gson();
			CreateMeeting zoomMeeting = gson.fromJson(response,CreateMeeting.class);
			return zoomMeeting;
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 安排会议
	 * @param host_id
	 * @param topic
	 * @param starttime
	 * @param password
	 * @param option_jbh
	 * @param d_minutes
	 * @return
	 */
	public static CreateMeeting createMeeting(String host_id, String topic, String password, String starttime, 
			Integer d_minutes, Boolean option_jbh, Boolean use_pmi) {
		try {
			String url = String.format(API_HOST+API_MEET_SCHEDULE, API_KEY, API_SECRET, 
					host_id, URLEncoder.encode(topic,"UTF-8"), password, URLEncoder.encode(starttime), d_minutes, option_jbh, "video", use_pmi);
			String response = HttpRequest.sendPost(url,"");
			if(response == null || response.contains("error")) {
				logger.debug("createMeeting failed.");
				return null;
			}
			Gson gson = new Gson();
			CreateMeeting zoomMeeting = gson.fromJson(response,CreateMeeting.class);
			return zoomMeeting;
			
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
	} 
	
	/**
	 * 更新会议信息
	 * @param host_id
	 * @param id
	 * @param topic
	 * @param password
	 * @param starttime
	 * @param d_minutes
	 * @param option_jbh
	 * @param use_pmi
	 * @return
	 */
	public ZoomUpdate updateMeeting(String host_id, String id, String topic, String password, String starttime, 
			int d_minutes, boolean option_jbh, boolean use_pmi) {
		try {
			String url = String.format(API_HOST+API_MEET_UPDATE, API_KEY, API_SECRET, 
					host_id, id, URLEncoder.encode(topic), password, URLEncoder.encode(starttime), d_minutes, option_jbh, "video", use_pmi);
			String response = HttpRequest.sendPost(url,"");
			if(response == null || response.contains("error")) {
				logger.debug("updateMeeting failed.");
				return null;
			}
			Gson gson = new Gson();
			ZoomUpdate zoomMeeting = gson.fromJson(response,ZoomUpdate.class);
			return zoomMeeting;
			
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
	} 
	
	/**
	 * 删除会议
	 * @param id
	 * @param host_id
	 * @return
	 */
	public ZoomDelete deleteMeeting(String id, String host_id) {
		try {
			String url = String.format(API_HOST+API_MEET_DELETE, API_KEY, API_SECRET, 
					host_id, id);
			String response = HttpRequest.sendPost(url,"");
			if(response == null || response.contains("error")) {
				logger.debug("deleteMeeting failed.");
				return null;
			}
			Gson gson = new Gson();
			ZoomDelete zoomMeeting = gson.fromJson(response,ZoomDelete.class);
			return zoomMeeting;
			
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 结束会议
	 * @param id
	 * @param host_id
	 * @return
	 */
	public static  ZoomEnd endMeeting(String id, String host_id) {
		try {
			String url = String.format(API_HOST+API_MEET_END, API_KEY, API_SECRET, 
					host_id, id);
			String response = HttpRequest.sendPost(url,"");
			if(response == null || response.contains("error")) {
				logger.debug("endMeeting failed.");
				return null;
			}
			Gson gson = new Gson();
			ZoomEnd result = gson.fromJson(response,ZoomEnd.class);
			return result;
			
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 获取会议信息
	 * @param id
	 * @param host_id
	 * @return
	 */
	public GetMeeting getMeeting(String id, String host_id) {
		try {
			String url = String.format(API_HOST+API_MEET_GET, API_KEY, API_SECRET, 
					host_id, id);
			String response = HttpRequest.sendPost(url,"");
			if(response == null || response.contains("error")) {
				logger.debug("getMeeting failed.");
				return null;
			}
			Gson gson = new Gson();
			return gson.fromJson(response, GetMeeting.class);
			
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 获取已安排会议列表
	 * @param host_id
	 * @param page_size
	 * @param page_number
	 * @return
	 */
	public MeetingList getMeetingList(String host_id, int page_size, int page_number) {
		try {
			String url = String.format(API_HOST+API_MEET_LIST, API_KEY, API_SECRET, 
					host_id, page_size, page_number);
			
			String response = HttpRequest.sendPost(url,"");
			if(response == null || response.contains("error")) {
				logger.debug("getMeetingList failed.");
				return null;
			}
			Gson gson = new Gson();
			MeetingList result = gson.fromJson(response,MeetingList.class);
			return result;
			
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 动态创建ZOOM ACCOUNT
	 * @param zoomuser
	 * @return 
	 */
	public static ZoomUser createUser(ZoomUser zoomuser) {
		try {
			String url = String.format(API_HOST+API_USER_CREATE, API_KEY,API_SECRET, 
					zoomuser.getEmail(),zoomuser.getPassword(), zoomuser.getFirst_name(), 
					"",zoomuser.getMeeting_capacity(), zoomuser.getDept());
			
			String response = HttpRequest.sendPost(url,"");
			logger.debug(response);
			if(response == null || response.contains("error")) {
				logger.debug("createUser failed.");
				//return null;
			}
			Gson gson = new Gson();
			return gson.fromJson(response, ZoomUser.class);
			
		} catch(Exception ex) {
			ex.printStackTrace();
			return null;
		}
	}
	/**
	 * 更新ZOOM用户
	 * @param zoomuser
	 * @return
	 */
	public static ZoomUpdate updateUser(ZoomUser zoomuser) {
		try {
			String url = String.format(API_HOST+API_USER_UPDATE, API_KEY,API_SECRET, 
					zoomuser.getType(),zoomuser.getId(), zoomuser.getFirst_name(), 
					zoomuser.getLast_name(),zoomuser.getMeeting_capacity(), zoomuser.getDept());
			
			String response = HttpRequest.sendPost(url,"");
			if(response == null || response.contains("error")) {
				logger.debug("updateUser failed.");
				return null;
			}
			Gson gson = new Gson();
			return gson.fromJson(response,ZoomUpdate.class);
		} catch(Exception ex) {
			ex.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 获取单个Zoom用户
	 * @param userId
	 * @return
	 */
	public static ZoomUser getByZoomUserId(String userId) {
		try {
			String url = String.format(API_HOST+API_USER_GET, API_KEY,API_SECRET, 
					userId);
			String response = HttpRequest.sendPost(url,"");
			if(response == null || response.contains("error")) {
				logger.debug("getByZoomUserId failed.");
				return null;
			}
			Gson gson = new Gson();
			ZoomUser zoomAccount = gson.fromJson(response,ZoomUser.class);
			return zoomAccount;
			
		} catch(Exception ex) {
			ex.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 通过邮件获取ZOOM用户
	 * @param email
	 * @return
	 */
	public ZoomUser getUserByEmail(String email) {
		try {
			String url = String.format(API_HOST+API_USER_GETBYEMAIL, API_KEY,API_SECRET, 
					email, 100);
			
			String response = HttpRequest.sendPost(url,"");
			if(response == null || response.contains("error")) {
				logger.debug("getUserByEmail failed.");
				return null;
			}
			Gson gson = new Gson();
			ZoomUser zoomAccount = gson.fromJson(response,ZoomUser.class);
			return zoomAccount;
		} catch(Exception ex) {
			ex.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 删除ZOOM用户
	 * @param userId
	 * @param email
	 * @return
	 */
	public static ZoomUserDelete deleteUser(String userId,String email) {
		try {
			String url = String.format(API_HOST+API_USER_DELETE, API_KEY,API_SECRET, 
					userId, email);
			
			String response = HttpRequest.sendPost(url,"");
			if(response == null ) {
				logger.debug("deleteUser failed.");
				return null;
			}
			Gson gson = new Gson();
			ZoomUserDelete result = gson.fromJson(response,ZoomUserDelete.class);
			return result;
			
		} catch(Exception ex) {
			ex.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 获取会议报表 历史会议信息
	 * @param userId
	 * @param from
	 * @param to
	 * @param pageSize
	 * @param pageNumber
	 * @return
	 */
	public MeetingUserReport getUserReport(String userId, String from, String to, int pageSize, int pageNumber) {
		try {
			String url = String.format(API_HOST+API_REPORT_GET_USERREPORT, API_KEY,API_SECRET, 
					URLEncoder.encode(from), URLEncoder.encode(to), pageSize, pageNumber);
			String response = HttpRequest.sendPost(url,"");
			if(response == null || response.contains("error")) {
				logger.debug("getUserReport failed.");
				return null;
			}
			Gson gson = new Gson();
			MeetingUserReport result = gson.fromJson(response,MeetingUserReport.class);
			return result;
			
		} catch(Exception ex) {
			ex.printStackTrace();
			return null;
		}
	}
	
	public static void main(String[] args) {
	/*	String url = String.format(API_HOST+API_USER_DELETE, API_KEY,API_SECRET, 
				"uAz3tL93Tt6f-v_6CasFMA","tempaccount_1@mengshi.com",  "");*/
	 
			String url = String.format(API_HOST+"v1/user/list?api_key=%s&api_secret=%s&data_type=JSON", API_KEY,API_SECRET, 
					 "");
		 String response = HttpRequest.sendPost(url,"");
		 System.out.println(response);
		/* JSONObject json = JSONObject.fromObject(response);
		 System.out.println(response);
		 JSONArray arr = json.getJSONArray("users");
		 for(int i=0;i<arr.size();i++){
			JSONObject temp =  arr.getJSONObject(i);
			String _id = temp.getString("id");
			String _email = temp.getString("email");
			if(_email.indexOf("tempaccount_")>=0){
			String url1 = String.format(API_HOST+API_USER_DELETE, API_KEY,API_SECRET, 
					_id,_email,  "");
			response = HttpRequest.sendPost(url1,"");
			 System.out.println(response);
			}
		 }*/
		
	}

}
